import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from'@/common/util/MinCache.js'
import { ACCESS_TOKEN,USER_NAME,USER_INFO,PAGE_PREVIOUS } from "@/common/util/constants"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userid:'',
    username: '',
    realname: '',
    welcome: '',
    avatar: '',
	mobile: '',
	pagePrevious: {},
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
	SET_USERID: (state, id) => {
		state.userid = id
	},
	SET_MOBILE: (state, mobile) => {
		state.mobile = mobile
	},
	SET_PAGE_PREVIOUS: (state, pagePrevious) => {
		state.pagePrevious = pagePrevious
	},
	SET_USERNAME: (state, username) => {
		state.username = username;
		let userInfo = uni.getStorageSync(USER_INFO);
		if(userInfo && userInfo.username != username) {
			userInfo.username = username;
			uni.setStorageSync(USER_INFO, userInfo);
		}
	},
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      console.log("mLogin",userInfo)
      return new Promise((resolve, reject) => {
       api.login(userInfo).then(response => {
          if(response.data.code ==200){ 
            const result = response.data.result
            const userInfo = result.userInfo
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,userInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_MOBILE', userInfo.mobile)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
            resolve(response)
          }else{
            resolve(response)
          }
        }).catch(error => {
			    // console.log("catch===>response",response)
          reject(error)
        })
      })
    },
	// 前台用户登录
	mLoginFrontEnd({ commit }, userInfo) {
	  // console.log("mLoginFrontEnd",userInfo)
	  return new Promise((resolve, reject) => {
	   api.loginFrontEnd(userInfo).then(response => {
	      if(response.data.code ==200){ 
	        const result = response.data.result;
	        const userInfo = result.userInfo;
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,userInfo);
	        commit('SET_TOKEN', result.token)
	        commit('SET_AVATAR', userInfo.avatar)
			commit('SET_USERID', userInfo.id)
	        commit('SET_NAME', { username: userInfo.username,realname: ''})
	        resolve(response)
	      }else{
	        resolve(response)
	      }
	    }).catch(error => {
			    // console.log("catch===>response",response)
	      reject(error)
	    })
	  })
	},
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
          if(response.data.code ==200){
            const result = response.data.result
            const userInfo = result.userInfo
            uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,userInfo);
	        commit('SET_TOKEN', result.token)
	        commit('SET_AVATAR', userInfo.avatar)
			commit('SET_USERID', userInfo.id)
	        commit('SET_NAME', { username: userInfo.username,realname: ''})
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        uni.removeStorageSync(ACCESS_TOKEN)
        api.logout(logoutToken).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },
    logoutFrontEnd({ commit, state }) {
      return new Promise((resolve, reject) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        api.logoutFrontEnd(logoutToken).then((response) => {
			console.log("response", response)
          if(response.statusCode =='200'){
			uni.removeStorageSync(ACCESS_TOKEN)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
	savePage({ commit }, pageToSave) {
		uni.setStorageSync(PAGE_PREVIOUS, pageToSave);
		commit('SET_PAGE_PREVIOUS', pageToSave)
	}
  },
  getters:{
    token: state => state.token,
  	username: state => {state.username=uni.getStorageSync(USER_INFO).username;return state.username},
  	realname: state => {state.realname=uni.getStorageSync(USER_INFO).realname; return state.realname},
  	avatar: state => {state.avatar = uni.getStorageSync(USER_INFO).avatar; return state.avatar},
  	userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
	mobile:state => {state.mobile=uni.getStorageSync(USER_INFO).mobile; return state.mobile},
	pagePrevious: state => {state.pagePrevious=uni.getStorageSync(PAGE_PREVIOUS); return state.pagePrevious},
  }
})
